/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Auntenticar_Usuario.java
 *
 * Created on 17-dic-2013, 21:59:54
 */
package Presentacion;

import Client.MailClient;
import Client.Usuario;
import Server.Archivo;
import Server.ListaServidores;
import Server.MailServer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 *
 * @author INFORMATICA
 */
public class Auntenticar_Usuario extends javax.swing.JFrame {
private ListaServidores Servidores;
private MailServer ServidorSeleccionado;
private Archivo MiArchivo=new Archivo();
    /** Creates new form Auntenticar_Usuario */
    public Auntenticar_Usuario() {
        initComponents();
         Archivo ar=new Archivo();
        try {
            try {       
//         
       Servidores=(ListaServidores)ar.Leer(new File("").getAbsolutePath()+"/Lista.list");
   
       for(int i=0;i<Servidores.ObtenerDimension();i++)
       {
           cmbServidor.addItem(Servidores.Obtener(i).ObtenerNombre());
       }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmbServidor = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        btnAcceder = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lblMensaje = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cmbServidor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbServidorItemStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Servidor:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Usuario:");

        btnAcceder.setFont(new java.awt.Font("Tahoma", 1, 11));
        btnAcceder.setText("Acceder");
        btnAcceder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAccederActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("Autenticar al Servidor");

        lblMensaje.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblMensaje.setText("......");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(106, 106, 106)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(38, 38, 38)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtUsuario)
                                .addComponent(cmbServidor, 0, 140, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(btnAcceder, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(181, 181, 181)
                        .addComponent(lblMensaje)))
                .addContainerGap(151, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(btnAcceder, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMensaje)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAccederActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAccederActionPerformed
        // TODO add your handling code here:
        String nombreUsuario=txtUsuario.getText();
             if(ServidorSeleccionado.ExisteUsuario(nombreUsuario))
              {   
                 lblMensaje.setText("Correcto");
                 
                 Gestionar_Mensajes gm=new Gestionar_Mensajes();
                 int ServidorSelec=cmbServidor.getSelectedIndex();
                String DireccionServidorSeleccionado=Servidores.Obtener(ServidorSelec).ObtenerDireccion();
          
                 gm.EstablecerCliente(ServidorSeleccionado.ObtenerCliente(nombreUsuario), ServidorSeleccionado,DireccionServidorSeleccionado);
                 gm.setVisible(true);
              }   
             else
                  lblMensaje.setText("El Usuario No Existe");
    }//GEN-LAST:event_btnAccederActionPerformed

    private void cmbServidorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbServidorItemStateChanged
        try {
            // TODO add your handling code here:
            int ServidorSelec=cmbServidor.getSelectedIndex();
            String DireccionServidorSeleccionado=Servidores.Obtener(ServidorSelec).ObtenerDireccion();
            ServidorSeleccionado=(MailServer)MiArchivo.Leer(DireccionServidorSeleccionado); 
//            ServidorSeleccionado=Servidores.Obtener(ServidorSelec);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Auntenticar_Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }
  
       
                
    }//GEN-LAST:event_cmbServidorItemStateChanged

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
//                try {
                    Auntenticar_Usuario p= new Auntenticar_Usuario();
                    p.setVisible(true);

                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAcceder;
    private javax.swing.JComboBox cmbServidor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
